package br.com.ps.bombertournament.game.action;

import java.util.ArrayList;
import java.util.List;

import br.com.ps.bombertournament.game.BaseAction;
import br.com.ps.bombertournament.game.BaseGameObject;
import br.com.ps.bombertournament.game.WrapperMap;
import br.com.ps.bombertournament.game.util.SoundCache;

import com.badlogic.gdx.math.MathUtils;

/**
 * Play the sound.
 * 
 * @author Rennan Stefan Boni - rennanboni@gmail.com
 */
public class PlaySoundAction extends BaseAction {

	private static final String FILE_PROPERTY = "file";
	private static final String FILES_PROPERTY = "files";
	
	private List<String> sounds;
	
	@Override
	public void create() {
		// Load all sound
		sounds = new ArrayList<String>();
		
		WrapperMap parameters = super.getParameters();
		if (parameters.containsKey(FILE_PROPERTY)) {
			String path = parameters.getString(FILE_PROPERTY);
			sounds.add(path);
			SoundCache.prepare(path);
		}
		
		if (parameters.containsKey(FILES_PROPERTY)) {
			List<WrapperMap> fileParameters = parameters.getList(FILES_PROPERTY);
			
			for (int i = 0; i < fileParameters.size(); i++) {
				String path = fileParameters.get(i).getString(FILE_PROPERTY);
				sounds.add(path);
				SoundCache.prepare(path);
			}
		}
	}

	@Override
	public void execute(BaseGameObject owner) {
		if (sounds != null) {
			// Playing the sound
			SoundCache.play(sounds.get(MathUtils.random.nextInt(sounds.size() - 1)));
		}
		
		super.setExecuted(true);
	}
	
	@Override
	public void undo(BaseGameObject owner) {
		// Do nothing
	}
}
